#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int mdconnect(char *ip_adress, char *ip_port){
  int server_len;
  struct sockaddr_in server_address;
  int result;
  int port;
  int fd_server_socket;
  
  if (((ip_port != NULL) && (ip_port[0] != '\0')) && sscanf(ip_port, "%d", &port))
  {
  }
  else
  {
    fprintf(stderr, "port: nespravny format pre port\n"); 
    return -1;
  }
  fd_server_socket = socket(AF_INET, SOCK_STREAM, 0);
  if (fd_server_socket == -1)
  {
    //perror("server_socket(socket())");
    //syslog(LOG_ERR, "server_socket(socket()) - %m");
    return -1;    
  }
  
  server_address.sin_addr.s_addr = inet_addr(ip_adress);
  server_address.sin_family = AF_INET;
  server_address.sin_port = htons(port);
  server_len = sizeof(server_address);
  
  result = connect(fd_server_socket, (struct sockaddr *) &server_address, server_len);
  if (result == -1)
  {
    //perror("server_socket(connect())");
    //syslog(LOG_ERR, "server_socket(connect()) - %m");
    return -1;    
  }
  
  return fd_server_socket; 
}